package com.hospital.workstation.webService.service.impl;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.hospital.common.core.domain.entity.SysUser;
import com.hospital.common.utils.StringUtils;
import com.hospital.system.mapper.SysDeptMapper;
import com.hospital.system.service.ISysUserService;
import com.hospital.workstation.mapper.FirstLevelMapper;
import com.hospital.workstation.mapper.PharmaceuticalCareDetailMapper;
import com.hospital.workstation.mapper.TwoLevelMapper;
import com.hospital.workstation.service.*;
import com.hospital.workstation.webService.config.Const;
import com.hospital.workstation.webService.domain.Response;
import com.hospital.workstation.webService.service.ZyyfyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author:HYZ
 * @Date:2023-2-28
 */
@Component
@Slf4j
public class ZyyfyServiceImpl implements ZyyfyService {

    @Autowired
    private IPatientBaseInfoService patientBaseInfoService;
    @Autowired
    private IHisAdmittingDiagInfoService hisAdmittingDiagInfoService;
    @Autowired
    private IHisChangeDepartmentService hisChangeDepartmentService;
    @Autowired
    private IHisVitalSignsService hisVitalSignsService;
    @Autowired
    private IHisMedicalOrdersService hisMedicalOrdersService;
    @Autowired
    private SysDeptMapper baseMapper;
    @Autowired
    private IHisMedicineStorageService hisMedicineStorageService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IFirstLevelService firstLevelService;
    @Autowired
    private ITwoLevelService twoLevelService;
    @Autowired
    private FirstLevelMapper firstLevelMapper;
    @Autowired
    private TwoLevelMapper twoLevelMapper;
    @Autowired
    private PharmaceuticalCareDetailMapper pharmaceuticalCareDetailMapper;

    /**
     * @param Input1 服务编码
     * @param Input2 消息xml
     * @return
     */
    @Override
    public String SendMessageInfo(String Input1, String Input2) {
        log.info("*******接收数据******");
        log.info("服务编码：" + Input1);
        log.info("消息xml：" + Input2);
        Response response = new Response();
        response.setHeader(System.currentTimeMillis() + "", "zdya");
        if (StrUtil.isBlank(Input1)) {
            response.setBody("1", "失败");
            return response.getXml();
        }
        if (StrUtil.isBlank(Input2)) {
            response.setBody("1", "失败");
            return response.getXml();
        }
        String result = "0";
        switch (Input1) {
            //接收患者医嘱信息
            case Const.MES_0055:
                result = SendMessageInfoService.AddOrders(Input2, patientBaseInfoService, hisMedicalOrdersService);
                break;
            //医嘱状态变更
            case Const.MES_0057:
                result = SendMessageInfoService.UpdateOrdersStatus(Input2, patientBaseInfoService, hisMedicalOrdersService);
                break;
            //患者基本信息
            case Const.MES_0058:
                result = SendMessageInfoService.PatientRegistry(Input2, patientBaseInfoService);
                break;
            //手术预约申请信息
            case Const.MES_0060:
                break;
            //挂号
            case Const.MES_0064:
                break;
            //住院登记
            case Const.MES_0066:
                result = SendMessageInfoService.InpatientEncounterStarted(Input2, patientBaseInfoService, hisAdmittingDiagInfoService, hisChangeDepartmentService, baseMapper);
                break;
            //取消住院登记
            case Const.MES_0067:
                break;
            //患者出院信息
            case Const.MES_0068:
                result = SendMessageInfoService.InpatientDischarge(Input2, patientBaseInfoService, hisChangeDepartmentService);
                break;
            // 取消患者出院
            case Const.MES_0069:
                break;
            // 接收诊断信息
            case Const.MES_0073:
                result = SendMessageInfoService.AddDiagnosis(Input2, patientBaseInfoService, hisAdmittingDiagInfoService);
                break;
            // 文档检索
            case Const.MES_0083:
                break;
            // 接收转科信息
            case Const.MES_0088:
                result = SendMessageInfoService.AdmTransaction(Input2, patientBaseInfoService, hisChangeDepartmentService);
                break;
            // 文档调阅
            case Const.MES_0093:
                break;
            // 删除诊断信息
            case Const.MES_0117:
                break;
            // 手术开始
            case Const.MES_0170:
                break;
            // 手术结束
            case Const.MES_0173:
                break;
            // 保存生命体征
            case Const.MES_0185:
                result = SendMessageInfoService.SaveVitalSigns(Input2, patientBaseInfoService, pharmaceuticalCareDetailMapper);
                break;
            // 药品材料字典表
            case Const.MES_0013:
                result = SendMessageInfoService.maintainHisMedicineStorage(Input2, hisMedicineStorageService);
                break;
            // 医嘱项目字典表（库存药品价格和单位）
            case Const.MES_0017:
                result = SendMessageInfoService.updateMedicineStoragePriceAndUnit(Input2, hisMedicineStorageService);
                break;
            // 接收检验结果
            case Const.MES_0078:
                result = SendMessageInfoService.receiveInspection(Input2, patientBaseInfoService, firstLevelMapper, twoLevelMapper);
                break;
            default:
                break;
        }

        if ("0".equals(result)) {
            response.setBody("0", "成功");
        } else {
            response.setBody("1", "失败");
        }


        return response.getXml();
    }

    /**
     * @param Input1 服务编码
     * @param Input2 消息xml
     * @return
     */
    @Override
    public String SendManagerDataInfo(String Input1, String Input2) {
        return null;
    }

    /**
     * 验证不良反应单点登录的票据
     *
     * @param ptickets 票据
     * @param pcode    工号
     * @return
     */
    @Override
    public String validateTicket(String ptickets, String pcode) {
        try {
            // 工号不能为空
            if (StringUtils.isBlank(pcode)) {
                return "-1";
            }
            // 根据工号查询用户信息
            SysUser sysUser = sysUserService.selectUserByHisUserNo(pcode);
            if (ObjectUtil.isNull(sysUser)) { // 用户不存在
                return "-1";
            }
            if (StringUtils.isBlank(sysUser.getHisUserName())) { // HIS用户名不能为空
                return "-1";
            }
            // 校验票据
            if (!BCrypt.checkpw(sysUser.getHisUserNo() + sysUser.getHisUserName(), ptickets)) { // 票据检验失败
                return "-1";
            }
            return sysUser.getHisUserName();
        } catch (Exception e) {
            log.error(e.getMessage());
            return "-1";
        }
    }

}
